package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecPsnorgEditBean;
import cn.ffcs.uec.staff.model.UecPsnorg;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecPsnorgEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecPsnorgEditBean bean = new UecPsnorgEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 
	 */
	private UecPsnorg uecPsnorg;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecPsnorgEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		List<NodeVo> nodeVoIsList = new ArrayList<NodeVo>();
		String[][] is = {{"Y", "是"}, {"N", "否"}};
		for (int i = 0; i < is.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(is[i][j]);
				nodeVo.setName(is[i][j + 1]);
			}
			nodeVoIsList.add(nodeVo);
		}
		//是否终止
		ListboxUtils.rendererForEdit(bean.getEndflag(),
				nodeVoIsList);
		//是否转入人员档案
		ListboxUtils.rendererForEdit(bean.getIndocflag(),
				nodeVoIsList);
		//是否核心员工
		ListboxUtils.rendererForEdit(bean.getOrgglbdef6(),
				nodeVoIsList);
		
		//人员类型
		List<NodeVo> nodeVos = new ArrayList<NodeVo>();
		String[][] type = {{"0", "新进"}, {"1", "变更"}, {"3", "退出"}};
		for (int i = 0; i < type.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(type[i][j]);
				nodeVo.setName(type[i][j + 1]);
			}
			nodeVos.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPsntype(),
				nodeVos);
	}

	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecPsnorgEditWindow().setTitle("新增组织关系");
			bean.getUecPsnorgEditDiv().setHeight("30%");
		}
		if ("view".equals(opType)) {
			this.bean.getUecPsnorgEditWindow().setTitle("查看组织关系");
			uecPsnorg = (UecPsnorg) arg.get("uecPsnorg");
			if (null != uecPsnorg) {
				PubUtil.fillBeanFromPo(uecPsnorg, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecPsnorgEditDiv(),true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecPsnorgEditWindow().setTitle("编辑组织关系");
			bean.getUecPsnorgEditDiv().setHeight("30%");
			uecPsnorg = (UecPsnorg) arg.get("uecPsnorg");
			if (null != uecPsnorg) {
				PubUtil.fillBeanFromPo(uecPsnorg, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
			uecPsnorg = UecPsnorg.newInstance();
			PubUtil.fillPoFromBean(bean, uecPsnorg);
			dateTransToString();
			uecPsnorg.setStaffId(uecStaffInfo.getStaffId());
			uecPsnorg.setDr(1L);
			uecPsnorg.addOnly();
			Events.postEvent("onOK", this.self, uecPsnorg);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecPsnorg);
			dateTransToString();
			uecPsnorg.updateOnly();
			Events.postEvent("onOK", this.self, uecPsnorg);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecPsnorgEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		 /*if (bean.getLastflag().getValue().length() > 1) {
			 return "最近记录标识只能一个字符长度！";*/
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourType().getValue())) {
		// return "填写奖励类型";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourMeas().getValue())) {
		// return "填写奖励措施";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourOrg().getValue())) {
		// return "填写奖励机构";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourRank().getValue())) {
		// return "填写奖励级别";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourGrade().getValue())) {
		// return "填写奖励档次";
		 //}
		return null;
	}
	/**
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			uecPsnorg.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		} 
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			uecPsnorg.setEnddate(formatter.format(bean.getEnddate()
					.getValue()));
		} 
		if (!StrUtil.isNullOrEmpty(bean.getJoinsysdate().getValue())) {
			uecPsnorg.setJoinsysdate(formatter.format(bean.getJoinsysdate()
					.getValue()));
		}
	}
	
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 奖励日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecPsnorg.getBegindate()) {
			bean.getBegindate()
					.setValue(sdf.parse(uecPsnorg.getBegindate()));
		}
		if (null != uecPsnorg.getEnddate()) {
			bean.getEnddate()
					.setValue(sdf.parse(uecPsnorg.getEnddate()));
		}
		if (null != uecPsnorg.getJoinsysdate()) {
			bean.getJoinsysdate()
					.setValue(sdf.parse(uecPsnorg.getJoinsysdate()));
		}
	}

}
